##################################
#Figure OB3 Plot
##################################

######################
#Packages
######################
library(rio)
library(tidyverse)
library(cowplot)


######Plot A: Probability of saying too extreme by pid and ideology, Pew and CNN
##Probabilities from STATA's margins command

pew_reps <- import("./Data/pew_cnn_stata_rep_predictions.dta")
pew_dems <- import("./Data/pew_cnn_stata_dem_predictions.dta")

#Cleaning/renaming/binding
pew_reps1 <- pew_reps %>%
  rename(pid1 = `_at1`, 
         ideol1 = `_at2`, 
         margins = `_margin`, 
         se = `_se`, 
         zstat = `_statistic`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`) %>%
  select(pid1, ideol1, margins, se, zstat, lower, upper) %>%
  mutate(target = "Republican Party",
         pid2 = factor(pid1, 
                       levels=c(-2,0,2), 
                       labels=c("Democrat", "Independent", "Republican")))

pew_dems1 <- pew_dems %>%
  rename(pid1 = `_at1`, 
         ideol1 = `_at2`, 
         margins = `_margin`, 
         se = `_se`, 
         zstat = `_statistic`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`) %>%
  select(pid1, ideol1, margins, se, zstat, lower, upper) %>%
  mutate(target = "Democratic Party",
         pid2 = factor(pid1, 
                       levels=c(-2,0,2), 
                       labels=c("Democrat", "Independent", "Republican")))

pew_comb <- rbind(pew_dems1, pew_reps1)


#Plotting
plota <- pew_comb %>%
  mutate(target = factor(target, 
                         levels=c("Republican Party", "Democratic Party"))) %>%
  ggplot(aes(x=ideol1, y=margins, shape = pid2, line=pid2)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  geom_line() + 
  theme_bw() + 
  labs(title = "Pew and CNN: Symbolic Ideology", 
       y = "Avg. Predicted Probability", 
       x = "Ideology", 
       shape = "Respondent PID" , 
       line = "Respondent PID") + 
  scale_x_continuous(labels=c("Very/nLib", "-1", "Moderate", 
                              "1" , "Very/nCons")) + 
  facet_wrap(~target) + 
  theme(legend.position = "bottom") + 
  scale_y_continuous(limits=c(0,1), breaks=c(0,0.25,0.5,0.75,1))

#############
##Plot B:D: Lucid
##############

######Plot B
lucid_reps_symb <- import("./Data/lucid_rep_sym.dta") %>%
  rename(pid1 = `_at1`, 
         ideol1 = `_at2`, 
         margins = `_margin`, 
         se = `_se`, 
         zstat = `_statistic`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`) %>%
  select(pid1, ideol1, margins, se, zstat, lower, upper) %>%
  mutate(target = "Republican Party",
         pid2 = factor(pid1, 
                       levels=c(-3,0,3), 
                       labels=c("Strong Dem", "Independent", "Strong Rep")))

lucid_dems_symb <- import("./Data/lucid_dem_sym.dta") %>%
  rename(pid1 = `_at1`, 
         ideol1 = `_at2`, 
         margins = `_margin`, 
         se = `_se`, 
         zstat = `_statistic`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`) %>%
  select(pid1, ideol1, margins, se, zstat, lower, upper) %>%
  mutate(target = "Democratic Party",
         pid2 = factor(pid1, 
                       levels=c(-3,0,3), 
                       labels=c("Strong Dem", "Independent", "Strong Rep")))

symb_comb <- rbind(lucid_dems_symb, lucid_reps_symb)

plotb <- symb_comb %>%
  mutate(target = factor(target, 
                         levels=c("Republican Party", "Democratic Party"))) %>%
  ggplot(aes(x=ideol1, y=margins, shape = pid2, line=pid2)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  geom_line() + 
  theme_bw() + 
  labs(title = "Lucid: Symbolic Ideology", 
       y = "Avg. Predicted Probability", 
       x = "Symbolic Ideology", 
       shape = "Respondent PID" , 
       line = "Respondent PID") + 
  facet_wrap(~target) + 
  theme(legend.position = "bottom") + 
  scale_x_continuous(breaks=c(-3:3), 
                     labels=c("Very/nLib", "-2", "-1", "Moderate", "1", "2", "Very/nCons")) + 
  scale_y_continuous(limits=c(0,1), breaks=c(0,0.25,0.5,0.75,1))

######Plot C
lucid_reps_op <- import("./Data/lucid_rep_op.dta") %>%
  rename(pid1 = `_at1`, 
         ideol1 = `_at2`, 
         margins = `_margin`, 
         se = `_se`, 
         zstat = `_statistic`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`) %>%
  select(pid1, ideol1, margins, se, zstat, lower, upper) %>%
  mutate(target = "Republican Party",
         pid2 = factor(pid1, 
                       levels=c(-3,0,3), 
                       labels=c("Strong Dem", "Independent", "Strong Rep")))

lucid_dems_op <- import("./Data/lucid_dem_op.dta") %>%
  rename(pid1 = `_at1`, 
         ideol1 = `_at2`, 
         margins = `_margin`, 
         se = `_se`, 
         zstat = `_statistic`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`) %>%
  select(pid1, ideol1, margins, se, zstat, lower, upper) %>%
  mutate(target = "Democratic Party",
         pid2 = factor(pid1, 
                       levels=c(-3,0,3), 
                       labels=c("Strong Dem", "Independent", "Strong Rep")))

op_comb <- rbind(lucid_dems_op, lucid_reps_op)

plotc <- op_comb %>%
  mutate(target = factor(target, 
                         levels=c("Republican Party", "Democratic Party"))) %>%
  ggplot(aes(x=ideol1, y=margins, shape = pid2, line=pid2)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  geom_line() + 
  theme_bw() + 
  labs(title = "Lucid: Operational Ideology", 
       y = "Avg. Predicted Probability", 
       x = "Operational Ideology", 
       shape = "Respondent PID" , 
       line = "Respondent PID") + 
  facet_wrap(~target) + 
  theme(legend.position = "bottom") + 
  scale_y_continuous(limits=c(0,1), breaks=c(0,0.25,0.5,0.75,1)) + 
  scale_x_continuous(breaks=c(-5:5))

######Plot D

lucid_reps_prox <- import("./Data/lucid_rep_prox.dta") %>%
  rename(pid1 = `_at1`, 
         ideol1 = `_at2`, 
         margins = `_margin`, 
         se = `_se`, 
         zstat = `_statistic`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`) %>%
  select(pid1, ideol1, margins, se, zstat, lower, upper) %>%
  mutate(target = "Republican Party",
         pid2 = factor(pid1, 
                       levels=c(-3,0,3), 
                       labels=c("Strong Dem", "Independent", "Strong Rep")))

lucid_dems_prox <- import("./Data/lucid_dem_prox.dta") %>%
  rename(pid1 = `_at1`, 
         ideol1 = `_at2`, 
         margins = `_margin`, 
         se = `_se`, 
         zstat = `_statistic`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`) %>%
  select(pid1, ideol1, margins, se, zstat, lower, upper) %>%
  mutate(target = "Democratic Party",
         pid2 = factor(pid1, 
                       levels=c(-3,0,3), 
                       labels=c("Strong Dem", "Independent", "Strong Rep")))

prox_comb <- rbind(lucid_dems_prox, lucid_reps_prox)

plotd <- prox_comb %>%
  mutate(target = factor(target, 
                         levels=c("Republican Party", "Democratic Party"))) %>%
  ggplot(aes(x=ideol1, y=margins, shape = pid2, line=pid2)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  geom_line() + 
  theme_bw() + 
  labs(title = "Lucid: Perceived Ideological Proximity", 
       y = "Avg. Predicted Probability", 
       x = "Ideological Proximity (Higher = Less)", 
       shape = "Respondent PID" , 
       line = "Respondent PID") + 
  facet_wrap(~target) + 
  theme(legend.position = "bottom") + 
  scale_y_continuous(limits=c(0,1), breaks=c(0,0.25,0.5,0.75,1)) + 
  scale_x_continuous(breaks=c(-5:5))


########Combined Plot

plot_grid(plota, plotb, plotc, plotd,
          labels=c("A", "B", "C", "D"))

ggsave("figure_ob3.png", 
       height=10, width=12, dpi=600)
